package com.example.serverfront;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
    public static String sendJSON(String url, String json) throws Exception {
        String res = "";
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//建立http客户端 然后做post请求
        HttpPost request = new HttpPost(url);
        StringEntity params = new StringEntity(json, HTTP.UTF_8);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        CloseableHttpResponse response = httpClient.execute(request);
        res = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        httpClient.close();
        System.out.println(res);
        return res;
    }

}
